-- 卡池管理器
local GachaManager = {}
local UINodes = require("Data.UINodes")
local MathUtils = require("Utils.MathUtils")
local rarity = require("Data.ItemRarity")


local map ={["Common"]="普通",["Uncommon"]="罕见",["Rare"]="普通",["Epic"]="普通",["Legendary"]="传说"}
local cardInfos = require("Data.CardInfo")
-- print(cardInfos)




-- 单次抽卡
function GachaManager:updateUI(role)
    LuaAPI.global_register_trigger_event({EVENT.CUSTOM_EVENT, "执行抽奖"}, function(name, unit, data)
        print("玩家执行抽奖")
        local roldId = role.get_roleid()
        local role1 = GameAPI.get_role(roldId)
        role1.set_node_visible(UINodes["抽奖中宝箱"], true) 
        role1.send_ui_custom_event("抽奖开始",nil)
    end)

   
    local mapArray = {}
    for key, value in pairs(map) do
        table.insert(mapArray, {key = key, value = value})
    end
   
    for i = 1, 5 do
        print(mapArray[i].value)
        role.set_label_text(UINodes["名称文本"..i],mapArray[i].value) 
        role.set_label_text(UINodes["概率文本"..i],"抽取权重:"..cardInfos[i].weight)
         
    end
end

-- 单次抽卡
function GachaManager:drawOnce()
    print("抽卡1次")
    
    local weights = {}
    for _, info in ipairs(cardInfos) do
		table.insert(weights,info.weight)
	end
    print("抽取结果")
    print(MathUtils.weightedRandomChoice(cardInfos,weights))
    return MathUtils.weightedRandomChoice(cardInfos,weights)
    
end





-- 显示抽卡结果
function GachaManager:showResult(role,resIndex)
    print("index:"..resIndex)
    print(cardInfos[resIndex])
   role.set_image_texture_by_key_with_auto_resize(UINodes["抽取结果图片"], 11000, false)
   local rankey = MathUtils.randomChoice(cardInfos[resIndex].ids)
   local createObj =role.get_ctrl_unit().create_equipment_to_slot(rankey,Enums.EquipmentSlotType.BACKPACK)
    print("创建结果")
    local name = createObj.get_name()
   print(createOjb)
   role.set_label_text(UINodes["抽取结果文本"], tostring(name)) 
   role.set_label_text(UINodes["抽取结果稀有度文本"],tostring(cardInfos[resIndex].weight)) 
end

-- 测试代码

return GachaManager
